package com.sqlcheck.enums;

import com.baomidou.mybatisplus.annotation.EnumValue;
import com.fasterxml.jackson.annotation.JsonValue;
import lombok.Getter;

/**
 * 任务类型枚举
 * 
 * @author Claude Code
 * @since 2025-09-04
 */
@Getter
public enum TaskType {

    /** 项目扫描 - 完整项目分析 */
    PROJECT_SCAN("PROJECT_SCAN", "项目扫描"),

    /** Git源码扫描 - 从Git仓库扫描源码中的SQL */
    GIT_SOURCE_SCAN("GIT_SOURCE_SCAN", "Git源码扫描"),

    /** 数据库DDL扫描 - 从数据库提取DDL进行分析 */
    DATABASE_DDL_SCAN("DATABASE_DDL_SCAN", "数据库DDL扫描"),

    /** 混合扫描 - 同时进行Git和数据库扫描 */
    MIXED_SCAN("MIXED_SCAN", "混合扫描"),

    /** 手动SQL分析 - 手动输入SQL进行分析 */
    MANUAL_SQL("MANUAL_SQL", "手动SQL分析");

    /** 数据库存储值 */
    @EnumValue
    private final String code;

    /** 显示名称 */
    private final String displayName;

    TaskType(String code, String displayName) {
        this.code = code;
        this.displayName = displayName;
    }

    /**
     * JSON序列化时使用的值
     */
    @JsonValue
    public String getCode() {
        return code;
    }

    /**
     * 根据代码获取枚举
     */
    public static TaskType fromCode(String code) {
        if (code == null) {
            return null;
        }

        for (TaskType type : values()) {
            if (type.code.equals(code)) {
                return type;
            }
        }

        // 兼容旧版本字符串映射
        switch (code) {
            case "GIT_SOURCE_SCAN":
                return GIT_SOURCE_SCAN;
            case "DATABASE_DDL_SCAN":
                return DATABASE_DDL_SCAN;
            default:
                throw new IllegalArgumentException("未知的任务类型: " + code);
        }
    }

    /**
     * 检查是否为Git相关任务
     */
    public boolean isGitRelated() {
        return this == GIT_SOURCE_SCAN || this == PROJECT_SCAN || this == MIXED_SCAN;
    }

    /**
     * 检查是否为数据库相关任务
     */
    public boolean isDatabaseRelated() {
        return this == DATABASE_DDL_SCAN || this == PROJECT_SCAN || this == MIXED_SCAN;
    }

    /**
     * 检查是否需要外部资源
     */
    public boolean requiresExternalResource() {
        return this != MANUAL_SQL;
    }
}